#include "header.h"
extern game world;
extern monster monDB[100];

void board::exploreRect(room space)
{
  for (int x=space.x;x<space.x+space.w;x++)
  {
    for (int y=space.y;y<space.y+space.h;y++)
    {
      data[x][y].explored=true;
    }
  }
}

bool board::movePlayer(int x, int y)
{
  //check if you're within screen bounds
  //check if you can walk onto that square
  //move there, explore space, check to scroll
  //reset character graphic
  //return true
  if(x>0 && y>0 && x<100 && y<100)
  {
    if(data[x][y].walkable==true)
    {
      playerx = x;
      playery = y; 
      exploreRect(room(x-1,y-1,3,3));
      scrollCamera(x,y);
      playershape=char(001);
      randomEncounter(x, y);
      updateRect(room(x-1-screenx,y-1-screeny,3,3));
      return true;
    }
    else
    {
      playershape=char(002);
      updateRect(room(x-1-screenx,y-1-screeny,3,3));
      return false;
      
    }
    
  } // end boundary if
}

void board::scrollCamera(int x, int y)
{
  //check if you need to scroll a direction
  //check if you can scroll that direction
  //scroll that direction
  //update the screen
  
  if (x-screenx>75) // you need to scroll right
  {
    if (screenx<20) // you can scroll right
    {
      screenx++;
      updateRect(room(0,0,80,25));
    }
  }
  
  if (x-screenx<5) // you need to scroll left
  {
    if (screenx>0) // you can scroll left
    {
      screenx--;
      updateRect(room(0,0,80,25));
    }
  }
  
  if (y-screeny>20) // you need to scroll down
  {
    if (screeny<80) // you can scroll down
    {
      screeny++;
      updateRect(room(0,0,80,25));
    }
  }
  
  if (y-screeny<5) // you need to scroll up
  {
    if (screeny>0) // you can scroll up
    {
      screeny--;
      updateRect(room(0,0,80,25));
    }
  }
}

void board::scrollCameraSilent(int x, int y)
{
  //check if you need to scroll a direction
  //check if you can scroll that direction
  //scroll that direction
  //update the screen
  
  if (x-screenx>75) // you need to scroll right
  {
    if (screenx<20) // you can scroll right
    {
      screenx++;
    }
  }
  
  if (x-screenx<5) // you need to scroll left
  {
    if (screenx>0) // you can scroll left
    {
      screenx--;
    }
  }
  
  if (y-screeny>20) // you need to scroll down
  {
    if (screeny<80) // you can scroll down
    {
      screeny++;
    }
  }
  
  if (y-screeny<5) // you need to scroll up
  {
    if (screeny>0) // you can scroll up
    {
      screeny--;
    }
  }
}

void board::centerCamera()
{
  for (int n=0;n<100;n++)
  {
    scrollCameraSilent(playerx,playery);
  }
  updateRect(room(0,0,80,25));
}

void board::randomEncounter(int x, int y)
{
  //Put stuff in here later
  if ((rand()%20)==0)
  {
    world.cmonsters.clear();
    world.cmonsters.push_back(monDB[0]);
    world.cmonsters.push_back(monDB[0]);
    world.cbattle = new battle(world.player, world.cmonsters);
  }
}
